/***************************operaupdater.cpp*******************************/
/*****************************************************************************/
/*******************Browsers Daily Build Updater************************/
/************Copyright (C) 2010-2011  Gianluigi Biancucci*************/
/*****************************************************************************/
/*****This is free software, and you are welcome to redistribute it****/
/*****and / or modify underthe terms of GPLV2 License.**************/
/*****This program comes with ABSOLUTELY NO WARRANTY*****/
/*****************************************************************************/

#include "OperaUpdater.h"
#include "singletondownloader.h"
#include "iostream"
#include "QFile"
#include <QMessageBox>
#include <QIcon>
#include <QLabel>
#include <QDir>
#include <QProcess>
#include <QStringList>
#include "operaupdater.h"
#include "QVariant"
#include "paths.h"

OperaUpdater::OperaUpdater(QString arch)

{


    downloader = SingletonDownloader::getInstance();
    QDir dir;
    dir = QDir();
    if(!dir.exists(OPERAPATH+"/"))dir.mkdir(OPERAPATH+"/");
    check32=new QFile(OPERAPATH+"/check32.txt");
    _32="win32";
   this->setArch(arch);
    BName = "Opera";
    lastError ="No Error";

}



void OperaUpdater::setArch(QString arch){
    //Forced to 32 bit version because there are no 64 bit version of Opera.(For further use)
    currCheckFile=check32;
    currOsType=_32;
}


bool OperaUpdater::download(QString version, QString * exePath){



    QByteArray opera;
    if( downloader->syncGet("http://snapshot.opera.com/windows/"+version+".exe",&opera)){
        QString buildNum=version;
        QFile* operaFile = new QFile(OPERAPATH+"/opera_"+buildNum.remove(0,buildNum.length()-13)+".exe");
       operaFile->open(QIODevice::WriteOnly);
       operaFile->write(opera);
       operaFile->close();
        delete operaFile;
        currCheckFile->open(QIODevice::WriteOnly);
        QVariant v = version;
        currCheckFile->write(v.toByteArray());
        currCheckFile->close();
        *exePath=OPERAPATH+"/opera_"+buildNum+".exe";
        return true;
    } else {
        lastError ="Undefined error when downloading Opera installer.\nCheck your connection settings and try again.";
        return false;

    }
}




bool OperaUpdater::getLatestVersion(QString * lastVer){
    //http://snapshot.opera.com/windows/partytime_11.00-1136/Opera_1100_1136_en.exe

    QByteArray  resBA;
    if(downloader->syncGet("http://my.opera.com/desktopteam/blog/",&resBA)){
        QString res(resBA);
         *lastVer=this->getContentBetween(res,"http://snapshot.opera.com/windows/",".exe");
        if((*lastVer).isEmpty()){lastError="Cannot parse correctly opera download link";return false;}
        return true;
    }
    else{
        lastError ="Undefined error when checking the latest version of Opera.\nCheck your connection settings and try again.";
        return false;
    }

}

bool OperaUpdater::getInstalledVersion(QString * installedVer){
    if(!currCheckFile->exists()){
        currCheckFile->open(QIODevice::WriteOnly);
        currCheckFile->close();
    }
    if(currCheckFile->open(QIODevice::ReadOnly)){
        QString ver(currCheckFile->readAll());
        currCheckFile->close();
        *installedVer=ver;
        return true;
    }
    else {
        lastError="unable to open Opera settings file ";
        return false;
    }
}

QString OperaUpdater::getContentBetween(QString source, QString startStr, QString endStr){


    int start = source.indexOf(startStr);
    if(start != -1){
        source.remove(0,start+startStr.length());
        int end = source.indexOf(endStr);
        if (end != -1){
            source.remove(end,source.length()-end);
            return source;
        }else return "";

    }else return "";
    }


QString OperaUpdater::getBrowserName(){

    return BName;

}

QString OperaUpdater::getLastError(){

    return lastError;

}
